﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmDm.
	/// </summary>
	public class frmDmhachtoan : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butKetthuc;
		private AccessData a;
		private DataTable dt=new DataTable();
		private DataTable dttk=new DataTable();
		private DataTable dttkch=new DataTable();
		private System.Windows.Forms.Button butMoi;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.Button butHuy;
		private long l_id;
		private int i_userid,loai;
		private string mmyy,user,sql1,sql2;
		private System.Windows.Forms.Label lstk;
		private System.Windows.Forms.ComboBox ttk;
		private System.Windows.Forms.TextBox stk;
		private System.Windows.Forms.TextBox stkch;
		private System.Windows.Forms.ComboBox ttkch;
		private System.Windows.Forms.Label lstkch;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDmhachtoan(AccessData acc,string _mmyy,string _sql1,string _sql2,int _loai,string title,string lbl1,string lbl2,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.Text=title;sql1=_sql1;sql2=_sql2;
			lstk.Text=lbl1;lstkch.Text=lbl2;
			a=acc;i_userid=userid;mmyy=_mmyy;loai=_loai;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDmhachtoan));
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butMoi = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.lstk = new System.Windows.Forms.Label();
            this.ttk = new System.Windows.Forms.ComboBox();
            this.stk = new System.Windows.Forms.TextBox();
            this.stkch = new System.Windows.Forms.TextBox();
            this.ttkch = new System.Windows.Forms.ComboBox();
            this.lstkch = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(8, -15);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 10;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(983, 445);
            this.dataGrid1.TabIndex = 19;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(520, 490);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(68, 25);
            this.butKetthuc.TabIndex = 11;
            this.butKetthuc.Text = "&Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butMoi
            // 
            this.butMoi.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butMoi.Image = ((System.Drawing.Image)(resources.GetObject("butMoi.Image")));
            this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butMoi.Location = new System.Drawing.Point(205, 490);
            this.butMoi.Name = "butMoi";
            this.butMoi.Size = new System.Drawing.Size(60, 25);
            this.butMoi.TabIndex = 8;
            this.butMoi.Text = "    &Mới";
            this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
            // 
            // butSua
            // 
            this.butSua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butSua.Image = ((System.Drawing.Image)(resources.GetObject("butSua.Image")));
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(268, 490);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(60, 25);
            this.butSua.TabIndex = 9;
            this.butSua.Text = "     &Sửa";
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butLuu
            // 
            this.butLuu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butLuu.Enabled = false;
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(331, 490);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(60, 25);
            this.butLuu.TabIndex = 6;
            this.butLuu.Text = "    &Lưu";
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butBoqua.Enabled = false;
            this.butBoqua.Image = ((System.Drawing.Image)(resources.GetObject("butBoqua.Image")));
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(394, 490);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(60, 25);
            this.butBoqua.TabIndex = 7;
            this.butBoqua.Text = "&Bỏ qua";
            this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // butHuy
            // 
            this.butHuy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butHuy.Image = ((System.Drawing.Image)(resources.GetObject("butHuy.Image")));
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(457, 490);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(60, 25);
            this.butHuy.TabIndex = 10;
            this.butHuy.Text = "     &Hủy";
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // lstk
            // 
            this.lstk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lstk.Location = new System.Drawing.Point(0, 440);
            this.lstk.Name = "lstk";
            this.lstk.Size = new System.Drawing.Size(152, 23);
            this.lstk.TabIndex = 0;
            this.lstk.Text = "Số TK :";
            this.lstk.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ttk
            // 
            this.ttk.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.ttk.BackColor = System.Drawing.SystemColors.HighlightText;
            this.ttk.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.ttk.Enabled = false;
            this.ttk.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ttk.Location = new System.Drawing.Point(272, 440);
            this.ttk.Name = "ttk";
            this.ttk.Size = new System.Drawing.Size(719, 21);
            this.ttk.TabIndex = 2;
            this.ttk.SelectedIndexChanged += new System.EventHandler(this.ttk_SelectedIndexChanged);
            this.ttk.KeyDown += new System.Windows.Forms.KeyEventHandler(this.stt_KeyDown);
            // 
            // stk
            // 
            this.stk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.stk.BackColor = System.Drawing.SystemColors.HighlightText;
            this.stk.Enabled = false;
            this.stk.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.stk.Location = new System.Drawing.Point(157, 440);
            this.stk.Name = "stk";
            this.stk.Size = new System.Drawing.Size(112, 21);
            this.stk.TabIndex = 1;
            this.stk.Validated += new System.EventHandler(this.stk_Validated);
            this.stk.KeyDown += new System.Windows.Forms.KeyEventHandler(this.stt_KeyDown);
            // 
            // stkch
            // 
            this.stkch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.stkch.BackColor = System.Drawing.SystemColors.HighlightText;
            this.stkch.Enabled = false;
            this.stkch.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.stkch.Location = new System.Drawing.Point(157, 464);
            this.stkch.Name = "stkch";
            this.stkch.Size = new System.Drawing.Size(112, 21);
            this.stkch.TabIndex = 4;
            this.stkch.Validated += new System.EventHandler(this.stkch_Validated);
            this.stkch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.stt_KeyDown);
            // 
            // ttkch
            // 
            this.ttkch.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.ttkch.BackColor = System.Drawing.SystemColors.HighlightText;
            this.ttkch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.ttkch.Enabled = false;
            this.ttkch.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ttkch.Location = new System.Drawing.Point(272, 464);
            this.ttkch.Name = "ttkch";
            this.ttkch.Size = new System.Drawing.Size(719, 21);
            this.ttkch.TabIndex = 5;
            this.ttkch.SelectedIndexChanged += new System.EventHandler(this.ttkch_SelectedIndexChanged);
            this.ttkch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.stt_KeyDown);
            // 
            // lstkch
            // 
            this.lstkch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lstkch.Location = new System.Drawing.Point(0, 464);
            this.lstkch.Name = "lstkch";
            this.lstkch.Size = new System.Drawing.Size(152, 23);
            this.lstkch.TabIndex = 3;
            this.lstkch.Text = "Số TK :";
            this.lstkch.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // frmDmhachtoan
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(999, 573);
            this.Controls.Add(this.stkch);
            this.Controls.Add(this.ttkch);
            this.Controls.Add(this.lstkch);
            this.Controls.Add(this.stk);
            this.Controls.Add(this.ttk);
            this.Controls.Add(this.lstk);
            this.Controls.Add(this.butHuy);
            this.Controls.Add(this.butBoqua);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.butSua);
            this.Controls.Add(this.butMoi);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.dataGrid1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmDmhachtoan";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục ngân hàng";
            this.Load += new System.EventHandler(this.frmDmhachtoan_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmDmhachtoan_KeyDown);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmDmhachtoan_Load(object sender, System.EventArgs e)
		{
			dttk=a.get_data(sql1).Tables[0];
			ttk.DisplayMember="TTK";
			ttk.ValueMember="ID";
			ttk.DataSource=dttk;

			dttkch=a.get_data(sql2).Tables[0];
			ttkch.DisplayMember="TTK";
			ttkch.ValueMember="ID";
			ttkch.DataSource=dttkch;

			user=a.user;
			load_grid();
			AddGridTableStyle();
			ref_text();
		}

		private void load_grid()
		{
			string sql="select a.*,b.stk as sotk,b.ttk,c.stk as sotkch,c.ttk as ttkch ";
			sql+=" from "+user+".a_hachtoan"+mmyy.Substring(2)+" a,"+user+".a_dmtk b,"+user+".a_dmtk c ";
			sql+=" where a.stk=b.id and a.stkch=c.id and a.loai="+loai+" order by b.stk,c.stk";
			dt=a.get_data(sql).Tables[0];
			dataGrid1.DataSource=dt;
		}

		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dt.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stk";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stkch";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotk";
			TextCol.HeaderText = lstk.Text;
			TextCol.Width = 150;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ttk";
			TextCol.HeaderText = "Tên";
			TextCol.Width = 250;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotkch";
			TextCol.HeaderText = lstkch.Text;
			TextCol.Width = 150;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ttkch";
			TextCol.HeaderText = "Tên";
			TextCol.Width = dataGrid1.Width-30-150-150-250;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			a.close();System.GC.Collect();this.Close();
		}

		private void ten_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void ena_object(bool ena)
		{
			dataGrid1.Enabled=!ena;
			stk.Enabled=ttk.Enabled=stkch.Enabled=ttkch.Enabled=ena;
			butMoi.Enabled=!ena;
			butSua.Enabled=!ena;
			butLuu.Enabled=ena;
			butBoqua.Enabled=ena;
			butHuy.Enabled=!ena;
			butKetthuc.Enabled=!ena;
		}

		private void butMoi_Click(object sender, System.EventArgs e)
		{
			l_id=0;			
			ttk.SelectedIndex=ttkch.SelectedIndex=-1;
			stk.Text=stkch.Text="";
			ena_object(true);
			stk.Focus();
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (dt.Rows.Count==0) return;
			l_id=long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
			ena_object(true);
			stk.Focus();
		}

		private bool kiemtra()
		{
			if (stk.Text=="")
			{
				MessageBox.Show("Yêu cầu nhập "+lstk.Text+"!",a.Msg);
				stk.Focus();
				return false;
			}
			if (stkch.Text=="")
			{
				MessageBox.Show("Yêu cầu nhập "+lstkch.Text+"!",a.Msg);
				stkch.Focus();
				return false;
			}
			if (stk.Text==stkch.Text)
			{
				MessageBox.Show("Không được trùng nhau !",a.Msg);
				stk.Focus();
				return false;
			}
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if (!kiemtra()) return ;
			l_id=(l_id==0)?a.getIdhachtoan():l_id;
			if (!a.upd_hachtoan(mmyy,l_id,long.Parse(ttk.SelectedValue.ToString()),long.Parse(ttkch.SelectedValue.ToString()),loai,i_userid))
			{
				MessageBox.Show("Không cập nhật thông tin "+this.Text.Trim()+" !",a.Msg);
				return;
			}
			load_grid();
			ref_text();
			ena_object(false);
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			ref_text();
			ena_object(false);
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
            try
            {
                if (a.get_data("select * from a_ct" + mmyy.Substring(2, 2) + " where iddongthoi=" + long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString())).Tables[0].Rows.Count != 0)
                {
                    MessageBox.Show("Số tài khoản " + stk.Text + " đang sử dụng không cho phép hủy !", a.Msg);
                    return;
                }
            }
            catch { }
			if (MessageBox.Show("Đồng ý hủy thông tin này !",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				a.execute_data("delete from "+user+".a_hachtoan"+mmyy.Substring(2)+" where id="+long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString()));
				load_grid();
			}
		}

		private void ref_text()
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				ttk.SelectedValue=dataGrid1[i,1].ToString().Trim();
				ttkch.SelectedValue=dataGrid1[i,2].ToString().Trim();
				stk.Text=dataGrid1[i,3].ToString();
				stkch.Text=dataGrid1[i,5].ToString();
			}
			catch{}
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text();
		}

		private void frmDmhachtoan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.F10) butKetthuc_Click(sender,e);
		}

		private void stt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");		
		}

		private void ttk_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==ttk && ttk.SelectedIndex!=-1) stk.Text=dttk.Rows[ttk.SelectedIndex]["stk"].ToString();
		}

		private void stk_Validated(object sender, System.EventArgs e)
		{
			DataRow r=a.getrowbyid(dttk,"stk='"+stk.Text+"'");
			if (r!=null) ttk.SelectedValue=r["id"].ToString();
			else 
			{
				stk.Text="";ttk.SelectedIndex=-1;
			}

		}

		private void stkch_Validated(object sender, System.EventArgs e)
		{
			DataRow r=a.getrowbyid(dttkch,"stk='"+stkch.Text+"'");
			if (r!=null) ttkch.SelectedValue=r["id"].ToString();
			else 
			{
				stkch.Text="";ttkch.SelectedIndex=-1;
			}
		}

		private void ttkch_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==ttkch && ttkch.SelectedIndex!=-1) stkch.Text=dttkch.Rows[ttkch.SelectedIndex]["stk"].ToString();		
		}

	}
}
